package com.learnkitchen.learnkitchenrecipes.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.learnkitchen.learnkitchenrecipes.base.BaseActivity;
import com.learnkitchen.learnkitchenrecipes.main.adapter.MainPagerAdapter;
import com.learnkitchen.learnkitchenrecipes.main.fragment.ClassificationFragment;
import com.learnkitchen.learnkitchenrecipes.main.fragment.FindFragment;
import com.learnkitchen.learnkitchenrecipes.main.fragment.MyFragment;
import com.learnkitchen.learnkitchenrecipes.main.fragment.RecipesFragment;
import com.learnkitchen.learnkitchenrecipes.utils.Logger;
import com.learnkitchen.learnkitchenrecipes.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ViewPager mMainViewPager;
    private TextView mMainBottomRecipesTv;
    private TextView mMainBottomClassificationTv;
    private TextView mMainBottomFindTv;
    private TextView mMainBottomMyTv;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public boolean isFullScreen() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);

        initView();
        initListener();
        initFragment();
        changeBottomSelectState(0);
    }

    private void initView() {
        mMainViewPager = findViewById(R.id.main_vp);
        mMainBottomRecipesTv = findViewById(R.id.main_bottom_recipes_tv);
        mMainBottomClassificationTv = findViewById(R.id.main_bottom_classification_tv);
        mMainBottomFindTv = findViewById(R.id.main_bottom_find_tv);
        mMainBottomMyTv = findViewById(R.id.main_bottom_my_tv);


    }

    private void initListener() {
        mMainBottomRecipesTv.setOnClickListener(this);
        mMainBottomClassificationTv.setOnClickListener(this);
        mMainBottomFindTv.setOnClickListener(this);
        mMainBottomMyTv.setOnClickListener(this);
    }

    private void initFragment() {
        if (fragments != null)
            fragments.clear();
        fragments.add(new RecipesFragment());
        fragments.add(new ClassificationFragment());
        fragments.add(new FindFragment());
        fragments.add(new MyFragment());

        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);
        mMainViewPager.setAdapter(adapter);
        mMainViewPager.setOffscreenPageLimit(fragments.size());
        mMainViewPager.setCurrentItem(0);
        mMainViewPager.addOnPageChangeListener(this);
    }

    private void changeBottomSelectState(int position) {
        mMainViewPager.setCurrentItem(position);
        mMainBottomRecipesTv.setSelected(0 == position);
        mMainBottomClassificationTv.setSelected(1 == position);
        mMainBottomFindTv.setSelected(2 == position);
        mMainBottomMyTv.setSelected(3 == position);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_bottom_recipes_tv:
                changeBottomSelectState(0);
                break;
            case R.id.main_bottom_classification_tv:
                changeBottomSelectState(1);
                break;
            case R.id.main_bottom_find_tv:
                changeBottomSelectState(2);
                break;
            case R.id.main_bottom_my_tv:
                changeBottomSelectState(3);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        changeBottomSelectState(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
